package com.ruoyi.spspublic.service.impl;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.spspublic.domain.TMisMessage;
import com.ruoyi.spspublic.domain.TMisReadRecord;
import com.ruoyi.spspublic.domain.vo.TMisReadRecordVO;
import com.ruoyi.spspublic.mapper.TMisReadRecordMapper;
import com.ruoyi.spspublic.service.ITMisReadRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 每一条单据的查看历史Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-17
 */
@Service
public class TMisReadRecordServiceImpl implements ITMisReadRecordService {
  @Autowired private TMisReadRecordMapper tMisReadRecordMapper;

  @Resource private RedisCache redisCache;

  /**
   * 查询每一条单据的查看历史
   *
   * @param id 每一条单据的查看历史ID
   * @return 每一条单据的查看历史
   */
  @Override
  public TMisReadRecord selectTMisReadRecordById(Long id) {
    return tMisReadRecordMapper.selectTMisReadRecordById(id);
  }

  /**
   * 查询每一条单据的查看历史列表
   *
   * @param tMisReadRecord 每一条单据的查看历史
   * @return 每一条单据的查看历史
   */
  @Override
  public List<TMisReadRecord> selectTMisReadRecordList(TMisReadRecord tMisReadRecord) {
    return tMisReadRecordMapper.selectTMisReadRecordList(tMisReadRecord);
  }

  /**
   * 新增每一条单据的查看历史
   *
   * @param tMisReadRecord 每一条单据的查看历史
   * @return 结果
   */
  @Override
  public int insertTMisReadRecord(TMisReadRecord tMisReadRecord) {
    return tMisReadRecordMapper.insertTMisReadRecord(tMisReadRecord);
  }

  /**
   * 修改每一条单据的查看历史
   *
   * @param tMisReadRecord 每一条单据的查看历史
   * @return 结果
   */
  @Override
  public int updateTMisReadRecord(TMisReadRecord tMisReadRecord) {
    return tMisReadRecordMapper.updateTMisReadRecord(tMisReadRecord);
  }

  /**
   * 批量删除每一条单据的查看历史
   *
   * @param ids 需要删除的每一条单据的查看历史ID
   * @return 结果
   */
  @Override
  public int deleteTMisReadRecordByIds(Long[] ids) {
    return tMisReadRecordMapper.deleteTMisReadRecordByIds(ids);
  }

  /**
   * 删除每一条单据的查看历史信息
   *
   * @param id 每一条单据的查看历史ID
   * @return 结果
   */
  @Override
  public int deleteTMisReadRecordById(Long id) {
    return tMisReadRecordMapper.deleteTMisReadRecordById(id);
  }

  @Override
  public void addRecord(TMisMessage tMisMessage) {
    /*String username = SecurityUtils.getUsername();
    *//*String ifExist =
        redisCache.getCacheObject(
            PartyCacheUtils.getCacheKey("BROWSE:" + username + "^" + tMisMessage.getCtBussId()));*//*
    // 拿不到则新建浏览记录，新增token
    if (null == ifExist || "".equals(ifExist)) {
      redisCache.setCacheObject(
          PartyCacheUtils.getCacheKey("BROWSE:" + username + "^" + tMisMessage.getCtBussId()),
          "EXIST",
          12,
          TimeUnit.HOURS);
      TMisReadRecord tMisReadRecord = new TMisReadRecord();
      tMisReadRecord.setCtFunNo(tMisMessage.getCtFunNo());
      tMisReadRecord.setCtBusinessNo(tMisMessage.getCtBussId());
      tMisReadRecord.setCtUsername(SecurityUtils.getUsername());
      tMisReadRecordMapper.insertTMisReadRecord(tMisReadRecord);
    } else {
      // 更新缓存有效期
      redisCache.setCacheObject(
          PartyCacheUtils.getCacheKey("BROWSE:" + username + "^" + tMisMessage.getCtBussId()),
          "EXIST",
          12,
          TimeUnit.HOURS);
    }*/
  }

  @Override
  public List<TMisReadRecordVO> getReadRecord(String functionNo, String businessNo) {
    return tMisReadRecordMapper.getReadRecord(functionNo, businessNo);
  }
}
